home *** CD-ROM | disk | FTP | other *** search
/ Delphi Magazine Collection 2001 / Delphi Magazine Collection 20001 (2001).iso / DISKS / Issue56 / System / RASCountryList.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  2000-02-29  |  3.3 KB  |  116 lines

  1. unit RASCountryList;
  2.  
  3. interface
  4.  
  5. uses
  6.   Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs;
  7.  
  8. type
  9.     TRASCountryList = class (TComponent)
  10.     private
  11.         { Private declarations }
  12.         fCountries, fDummy1: TStrings;
  13.         fCountryName: String;
  14.         fCountryDialCode, fCountryID, fDummy2: Integer;
  15.         procedure SetCountryID (Value: Integer);
  16.         procedure SetCountryName (const Value: String);
  17.     protected
  18.         { Protected declarations }
  19.     public
  20.         { Public declarations }
  21.         constructor Create (AOwner: TComponent); override;
  22.         destructor Destroy; override;
  23.         procedure Refresh;
  24.     published
  25.         { Published declarations }
  26.         property Countries: TStrings read fCountries write fDummy1 stored False;
  27.         property CountryName: String read fCountryName write SetCountryName;
  28.         property CountryDialCode: Integer read fCountryDialCode write fDummy2;
  29.         property CountryID: Integer read fCountryID write SetCountryID;
  30.     end;
  31.  
  32. procedure Register;
  33.  
  34. implementation
  35.  
  36. uses Registry;
  37.  
  38. constructor TRASCountryList.Create (AOwner: TComponent);
  39. begin
  40.     Inherited Create (AOwner);
  41.     fCountries := TStringList.Create;
  42.     Refresh;
  43.     // Why should the US have everything it's way? :-)
  44.     SetCountryName ('United Kingdom');
  45. end;
  46.  
  47. destructor TRASCountryList.Destroy;
  48. begin
  49.     fCountries.Free;
  50.     Inherited Destroy;
  51. end;
  52.  
  53. procedure TRASCountryList.Refresh;
  54. const
  55.     CListReg = '\SOFTWARE\Microsoft\Windows\CurrentVersion\Telephony\Country List';
  56. var
  57.     Idx: Integer;
  58.     Reg: TRegistry;
  59.     ObjectData: Integer;
  60.     IDNames: TStringList;
  61. begin
  62.     Reg := TRegistry.Create;
  63.     try
  64.         Reg.RootKey := hKey_Local_Machine;
  65.         if Reg.OpenKey (CListReg, False) then begin
  66.             fCountries.Clear;
  67.             // Set Sorted = False for speed....
  68.             TStringList (fCountries).Sorted := False;
  69.             IDNames := TStringList.Create;
  70.             try
  71.                 Reg.GetKeyNames (IDNames);
  72.                 for Idx := 0 to IDNames.Count - 1 do
  73.                     if Reg.OpenKey (CListReg + '\' + IDNames [Idx], False) then begin
  74.                         ObjectData := MakeLong (Reg.ReadInteger ('CountryCode'), StrToInt (IDNames [Idx]));
  75.                         fCountries.AddObject (Reg.ReadString ('Name'), TObject (ObjectData));
  76.                     end;
  77.             finally
  78.                 IDNames.Free;
  79.                 TStringList (fCountries).Sorted := True;
  80.             end;
  81.         end;
  82.     finally
  83.         Reg.Free;
  84.     end;
  85. end;
  86.  
  87. procedure TRASCountryList.SetCountryName (const Value: String);
  88. var
  89.     Idx: Integer;
  90. begin
  91.     Idx := fCountries.IndexOf (Value);
  92.     if Idx <> -1 then begin
  93.         fCountryName := fCountries [Idx];
  94.         fCountryDialCode := Integer (fCountries.Objects [Idx]) and $ffff;
  95.         fCountryID := Integer (fCountries.Objects [Idx]) shr 16;
  96.     end;
  97. end;
  98.  
  99. procedure TRASCountryList.SetCountryID (Value: Integer);
  100. var
  101.     Idx: Integer;
  102. begin
  103.     for Idx := 0 to fCountries.Count - 1 do
  104.         if Value = Integer (fCountries.Objects [Idx]) shr 16 then begin
  105.             SetCountryName (fCountries [Idx]);
  106.             Exit;
  107.         end;
  108. end;
  109.  
  110. procedure Register;
  111. begin
  112.     RegisterComponents ('DelphiMag', [TRASCountryList]);
  113. end;
  114.  
  115. end.
  116.